// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var router = require("../hash/pageRouter");
	var fileUuid = [];
	var css_product = require("./constractLogisticsAdd.css");
	var productId = "";
	var copyState;
	var init = function(page) {
		if (page != null) {
			copyState = page.params.copyState;
			productId = page.params.productId;
			if (productId != null) {
				databind.fillInput($("#constractLogisticsAdd"), {
					action: "inquireFinanceProductDetail.json?productId=" + productId,
					async: false
				});
				var fs = $("#logisticsIntroUuid").val().split(",");
				for (var j = 0; j < fs.length; j++) {
					if ($.trim(fs[j]) != "") {
						$("#constractLogisticsAdd .fs").append('<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + fs[j] + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + fs[j] + '" class="showProductIntro">产品附件</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>');
					}
				}
			}
		}
		router.init();
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//返回物流合同
		$(".constractLogisticsAdd").unbind("click").click(function() {
			router.route("constractLogisticsManage/constractLogisticsManage.html");
		});

		//自动补全
		$("#productLogisticsName").autocomplete(seajs.data.handler + "inquireLogisticsCompanyListByCompanyName.json", {
			minChars: 1,
			max: 10,
			width: 120,
			matchCase: false, //不区分大小写
			scroll: true,
			dataType: 'json',
			scrollHeight: 500,
			//此处为传递而外的参数
			extraParams: {
				pageSize: 1000,
				companyName: function(){
					return $("#productLogisticsName").val();
				},
				appKey: base.getData("appKey"),
				sessionUuid: base.getData("sid")
			},
			//需要把data转换成json数据格式                      
			parse: function(data) {
				return $.map(eval(data.datas), function(row) {
					return {
						data: row,
						value: row.companyName, //此处无需把全部列列出来，只是两个关键列
						result: row.companyName
					}
				});
			},
			formatItem: function(data, i, total) {
				if ($("#productLogisticsName").hasClass('nonemorelist')) {
					$("#productLogisticsName").removeClass('nonemorelist');
					return;
				};
				return "<table><tr><td>" + data.companyName + "</td></tr></table>";
			},
			formatMatch: function(data, i, total) {
				return data.companyName;
			},
			formatResult: function(data, value) {
				return data.companyName;
			}
		}).result(function(event, data, formatted) { //回调 
			$('#productLogisticsName').val(data.companyName);
			if ($("#productLogisticsName").hasClass('nonemorelist')) {
				$("#productLogisticsName").removeClass('nonemorelist');
			} else {
				$("#productLogisticsName").addClass('nonemorelist');
			};
			$("#companyId").val(data.companyId);
		});


		$("#constractLogisticsAdd").on("change", "#constractLogisticsIntro", function() {
			uploadFile();
		});

		$("#constractLogisticsAdd").on("click", ".deleteProductIntro", function() {
			$(this).parent().remove();
			fileUuid = [];
			for (var j = 0; j < $("#constractLogisticsAdd .showProductIntroDiv .mas").length; j++) {
				fileUuid.push($($("#constractLogisticsAdd .showProductIntroDiv .mas")[j]).attr("fileUuid"));
			}
			$("#logisticsIntroUuid").val(fileUuid);
		});

		$("#constractNext").unbind("click").click(function() {
			fileUuid = [];
			for (var j = 0; j < $("#constractLogisticsAdd .showProductIntroDiv .mas").length; j++) {
				fileUuid.push($($("#constractLogisticsAdd .showProductIntroDiv .mas")[j]).attr("fileUuid"));
			}
			$("#logisticsIntroUuid").val(fileUuid);

			var sDate = new Date($("#logisticsStartDate").val().replace(/-/g, "//"));
			var eDate = new Date($("#logisticsEndDate").val().replace(/-/g, "//"));

			if (sDate > eDate) {
				base.alert(2, "结束日期不能小于开始日期！");
			} else {
				if (productId != null) {
					if (copyState == 1) {
						databind.submitDom($("#constractLogisticsAdd"), function(data) {
							router.route("constractLogisticsManage/constractLogisticsCharge.html?productId=" + productId);
						});
					} else {
						$("#constractLogisticsAdd").attr("action", "updateFinanceProduct");
						databind.submitDom($("#constractLogisticsAdd"), function(data) {
							router.route("constractLogisticsManage/constractLogisticsCharge.html?productId=" + productId);
						});
					}
				} else {
					databind.submitDom($("#constractLogisticsAdd"), function(data) {
						router.route("constractLogisticsManage/constractLogisticsCharge.html?productId=" + data.datas[0].productId);
					});
				}
			}
		});

	};

	//上传文件
	var uploadFile = function() {
		if ($("#constractLogisticsIntro").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'constractLogisticsIntro',
				dataType: 'json',
				success: function(data) {
					var str = '<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">' + data.data[0].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>'
					$("#constractLogisticsAdd .constractIntro").before(str);
				},
				complete: function(xml) {
					$("#constractLogisticsIntro").change(function() {
						uploadFile();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};

	return {
		init: init
	};
});